#include <brpc/server.h>
#include <butil/logging.h> //关闭日志
#include "main.pb.h"
// 1、创建子类，继承于EchoService，并实现rpc调用的业务功能
class EchoServiceImpl : public example::EchoService
{
    void Echo(google::protobuf::RpcController *controller,
              const ::example::EchoRequest *request,
              ::example::EchoResponse *response,
              ::google::protobuf::Closure *done)
    {
        brpc::ClosureGuard rpc_guard(done);
        std::cout << "收到消息：" << request->message() << std::endl;
        std::string str = request->message() + "--这是响应! ";
        response->set_message(str);
    }
};
int main()
{
    //关闭brpc默认日志输出
    logging::LoggingSettings settings;
    settings.logging_dest = logging::LoggingDestination::LOG_TO_NONE; 
    logging::InitLogging(settings);
    // 2、构造服务器对象
    brpc::Server server;
    // 3、向服务器对象中，新增EchoServiceImpl服务
    EchoServiceImpl echo_service;
    if(server.AddService(&echo_service,brpc::ServiceOwnership::SERVER_DOESNT_OWN_SERVICE) == -1)
    {
        std::cout << "添加服务失败" << std::endl;
        return -1;
    }
    brpc::ServerOptions options; 
    // 4、启动服务器
    options.idle_timeout_sec = -1; //连接空闲超时事件-超时后连接被关闭
    options.num_threads = 1; //IO线程数量
    if(server.Start(8888,&options) == -1)
    {
        std::cout << "启动服务器失败" << std::endl;
        return -1;
    }
    server.RunUntilAskedToQuit(); //休眠等待运行结束
    return 0;
}
